<?php
/**
 * Description of PresentacionP
 *
 * @author Nelifer
 */
include_once('BDManager.class.php');

class Usuario {
    var $con;
    function Usuario(){
         $this->con=new BDManager();
    }

    function insertar($campos){
        if($this->con->conectar()==true){
            $sql = "INSERT INTO usuarios (usu_nombre, usu_apellido, usu_email, usu_login, usu_password, usu_fechacreacion, usu_horacreacion ) ".
                    "VALUES ('".$campos[0]."', '".$campos[1]."', '".$campos[2]."', '".$campos[3]."', '".$campos[4]."', '".$campos[5]."', '".$campos[6]."')";
            return mysql_query($sql);
        }
    }

    function actualizar($campos, $id){
        if($this->con->conectar()==true){
            $sql = "UPDATE usuarios SET usu_nombre = '".$campos[0]."', usu_apellido = '".$campos[1]."', ". 
                    "usu_password = '".$campos[3]."', usu_email = '".$campos[2]."' WHERE usu_id = ".$id;
            return mysql_query($sql);
        }
    }

    public function getConsulta($id){
        if($this->con->conectar()==true){
            return mysql_query("SELECT * FROM usuarios WHERE usu_id=".$id);
        }
    }

    function eliminar($id){
        if($this->con->conectar()==true){
                return mysql_query("DELETE FROM usuarios WHERE usu_id=".$id);
        }
    }
    private function countRec($fname,$tname,$where) {
        $con = null;
        $sql = "SELECT count($fname) FROM $tname $where";
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $result = mysql_query($sql);
        }
        while ($row = mysql_fetch_array($result)) {
            return $row[0];
        }
    }
    
    public function consultar_grid($page=null, $rp=null, $sortname=null, $sortorder=null, $query=null, $letter_pressed=null, $qtype=null){
        if (!$sortname) $sortname = 'name';
        if (!$sortorder) $sortorder = 'desc';
        if($query!=''){
             $where = "WHERE `".$qtype."` LIKE '%".$query."%' ";
        } else {
            $where ='';
        }
        if($letter_pressed!=''){
            $where = "WHERE `".$qtype."` LIKE '".$letter_pressed."%' ";	
        }
        if($letter_pressed=='#'){
            $where = "WHERE `".$qtype."` REGEXP '[[:digit:]]' ";
        }
        $sort = "ORDER BY $sortname $sortorder";

        if (!$page) $page = 1;
        if (!$rp) $rp = 10;
        $start = (($page-1) * $rp);
        $limit = "LIMIT $start, $rp";
        $sql = "SELECT usu_id, usu_nombre, usu_apellido, usu_email, usu_login, usu_fechacreacion FROM usuarios $where $sort $limit";
        $con = $this->con->conectar();
        $result = mysql_query($sql);
        
        $total = $this->countRec('usu_id','usuarios',$where);
        // Return JSON data
        $data = array();
        $data['page'] = $page;
        $data['total'] = $total;
        $data['rows'] = array();
        while ($row = mysql_fetch_assoc($result)) {
            $fecha = substr($row['usu_fechacreacion'], 6,7) . '/'. substr($row['usu_fechacreacion'], 4,-2) . '/'. substr($row['usu_fechacreacion'], 0,4);
            $data['rows'][] = array(
            'id' => $row['usu_id'],
            'cell' => array($row['usu_id'], $row['usu_login'], $row['usu_nombre'], $row['usu_apellido'], $row['usu_email'], $fecha)
            );
        }
         mysql_free_result($result);
        echo json_encode($data);
    }
    /*public function getItemsPresentacionP(){
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $param = trim($_GET['term']);

            $sql = "SELECT pre_id, pre_nombre FROM presentacionesproductos where pre_nombre like '%".$param ."%' OR '".$param."' = ''";
            $result = mysql_query($sql);

            $data   = array();
            while ($row = mysql_fetch_assoc($result)) {
                $row_array['id'] = $row['pre_id'];
                $row_array['value'] = $row['pre_nombre'];
                $row_array['abbrev'] = $row['pre_nombre'];
                array_push($data,$row_array);

            }
        }
        echo json_encode($data);
        
    }*/
}

?>
